﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Minestorm.Retro.GameLogic;

namespace Minestorm.Retro.Entities
{
    public class MineLayer : Entity
    {
        private GraphicsDevice _graphics;
        private const int MoveFrames = 50;
        private int _currentMoveFrame = 0;
        private Vector2 _stepVector;
        private bool _first;

        public MineLayer(GraphicsDevice graphics, Vector2 position)
            : base(graphics, position)
        {
            _graphics = graphics;

            Points = 1000;
            _first = true;

            PlotNewTarget(Vector2.Zero);
        }

        public override void Initialize()
        {
            var verticies = new VertexPositionColor[6];
            verticies[0] = new VertexPositionColor(new Vector3(-.75f, .25f, 0), Constants.VectorColor);
            verticies[1] = new VertexPositionColor(new Vector3(.75f, .25f, 0), Constants.VectorColor);
            verticies[2] = new VertexPositionColor(new Vector3(1, -.25f, 0), Constants.VectorColor);
            verticies[3] = new VertexPositionColor(new Vector3(2.5f, -1, 0), Constants.VectorColor);
            verticies[4] = new VertexPositionColor(new Vector3(-2.5f, -1, 0), Constants.VectorColor);
            verticies[5] = new VertexPositionColor(new Vector3(-1, -.25f, 0), Constants.VectorColor);

            SetVerticies(verticies);

            SetIndicies(
                new NamedShortArray("minelayer", true, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 0),
                new NamedShortArray("minelayer_detail", false, 5, 2));
        }

        public Boolean CheckLineCollision(Vector2 ls, Vector2 le)
        {
            return CollisionHelper.CheckLineCollision(ls, le, GetWorld("minelayer"), 6, Verticies);
        }

        public Boolean CheckPointCollision(Vector3[] points, out int vectorIndex)
        {
            return CollisionHelper.CheckPointCollision(points, GetWorld("minelayer"), 6, Verticies, out vectorIndex);
        }

        public override Matrix GetWorld(string indexBufferName)
        {
            return Matrix.Identity * Matrix.CreateScale(Constants.Ship.Scale) * Matrix.CreateTranslation(new Vector3(Position, 0f));
        }

        public void PlotNewTarget(Vector2? target)
        {
            var step = 1 / (float)MoveFrames;

            var targetVector = (target ?? (Vector2?)RandomHelper.GetVectorWithinBounds()).Value;

            var currentVector = Position;

            var stepX = (targetVector.X - currentVector.X) * step;
            var stepY = (targetVector.Y - currentVector.Y) * step;

            _stepVector = new Vector2(stepX, stepY);
        }

        public bool LayingStar { get; private set; }

        public void Move()
        {
            LayingStar = false;

            if (_currentMoveFrame == MoveFrames)
            {
                LayingStar = !_first;
                _currentMoveFrame = 0;
                PlotNewTarget(null);

                _first = false;
            }
            else
            {
                Position += _stepVector;

                _currentMoveFrame++;
            }
        }
    }
}
